{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, date\n",
    "from gs_quant.instrument import IRSwap, IRSwaption\n",
    "from gs_quant.backtests.triggers import PeriodicTrigger, PeriodicTriggerRequirements\n",
    "from gs_quant.backtests.actions import AddTradeAction, HedgeAction\n",
    "from gs_quant.backtests.generic_engine import GenericEngine\n",
    "from gs_quant.backtests.strategy import Strategy\n",
    "from gs_quant.risk import Price, IRDelta\n",
    "from gs_quant.common import Currency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize session\n",
    "from gs_quant.session import GsSession\n",
    "\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sell swaption monthly, delta hedge monthly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define backtest dates\n",
    "start_date = date(2021, 6, 1)\n",
    "end_date = datetime.today().date()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define instrument for strategy\n",
    "\n",
    "# IR Swaption\n",
    "option = IRSwaption(expiration_date='1m', termination_date='30y', notional_currency=Currency.EUR, buy_sell='Sell')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Risk Trigger: based on frequency threshold, delta hedge by swap trade\n",
    "\n",
    "# Define frequency for adding trade\n",
    "freq_add = '1m'\n",
    "trig_req = PeriodicTriggerRequirements(start_date=start_date, end_date=end_date, frequency=freq_add)\n",
    "action_add = AddTradeAction(option, 'expiration_date')\n",
    "\n",
    "# Define trade to hedge Delta\n",
    "freq_hedge = '1m'\n",
    "swap_hedge = IRSwap(termination_date='30y', notional_currency=Currency.EUR, notional_amount=100e6, name='30yhedge')\n",
    "hedge_risk = IRDelta(aggregation_level='Type', currency=Currency.USD)\n",
    "action_hedge = HedgeAction(hedge_risk, swap_hedge)\n",
    "\n",
    "# Starting with empty portfolio (first arg to Strategy), apply actions in order on trig_req\n",
    "triggers = PeriodicTrigger(trig_req, [action_add, action_hedge])\n",
    "strategy = Strategy(None, triggers)\n",
    "\n",
    "# Run backtest daily\n",
    "GE = GenericEngine()\n",
    "# The results will be in local ccy, EUR in this case. To change them to USD, specify result_ccy in the backtest args\n",
    "backtest = GE.run_backtest(strategy, start=start_date, end=end_date, frequency='1b', show_progress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# View backtest trade ledger\n",
    "backtest.trade_ledger()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# View results summary\n",
    "backtest.result_summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot results\n",
    "df = backtest.result_summary\n",
    "df['Performance'] = df[Price] + df['Cumulative Cash']\n",
    "df.plot(figsize=(10, 10)).legend(bbox_to_anchor=(1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
